package com.junjiao.util.dao.paging;

/**
 *说明：
 * 
 *构造HQL查询条件参数
 * 
 *@创建：作者:Qi 创建时间：Sep 7, 2009
 *@修改历史：[序号](Qi Sep 7, 2009)<修改说明>
 */
public class QueryWhere {

	public final static String OP_EQ = "=";
	public final static String OP_LIKE = "like";
	public final static String OP_NOT_LIKE = "not like";
	public final static String OP_IN = "in";
	public final static String OP_GT = ">";
	public final static String OP_LT = "<";
	public final static String OP_GE = ">=";
	public final static String OP_LE = "<=";

	private String propertyName;// 属性名

	private String condition;// 符号

	private Object propertyValue;// 属性值

	private String andOr = "and";// 是and还是or,默认是and

	public QueryWhere() {
	}

	/**
	 * 构造函数:默认是用and连接hql
	 * 
	 * @param propertyName
	 *            : 属性名
	 * @param condition
	 *            : 符号 > = 等
	 * @param propertyValue
	 *            : 属性值
	 */
	public QueryWhere(String propertyName, String condition,
			Object propertyValue) {
		this.andOr = "and";
		this.propertyName = propertyName;
		this.condition = condition;
		this.propertyValue = propertyValue;
	}

	/**
	 * 构造函数:自己选择用and或者or连接hql,不过一般情况下不需要用or,用or可能会引发一些问题
	 * 
	 * @param andOr
	 *            : AND/OR
	 * @param propertyName
	 *            : 属性名
	 * @param condition
	 *            : 符号 > = 等
	 * @param propertyValue
	 *            : 属性值
	 */
	@Deprecated
	public QueryWhere(String andOr, String propertyName, String condition,
			Object propertyValue) {
		this.andOr = andOr;
		this.propertyName = propertyName;
		this.condition = condition;
		this.propertyValue = propertyValue;
	}

	/** 获取查询条件的组合字符串 **/
	public StringBuffer getQueryWhereStr(String otherName) {
		StringBuffer sb = new StringBuffer();
		if (propertyValue == null || propertyValue.toString().equals(""))
			return sb;

		sb.append(andOr).append(" ")// andor
				.append(otherName).append(".");// 别名.
		if (propertyName.contains(".")) {// 如果属性名原本就包含了一个.(这个.是从页面传递过来的,如:实体.属性名)
			// 截取掉.前面的东东
			propertyName = propertyName.substring(
					propertyName.lastIndexOf(".") + 1, propertyName.length());
		}
		sb.append(propertyName).append(" ")// 属性名
				.append(condition).append(" ");// 条件符号
		if (condition.equals(OP_LIKE)) {// like
			sb.append("'").append(propertyValue.toString().trim()).append("'");
		} else if (condition.equals(OP_NOT_LIKE)) {
			sb.append("'").append(propertyValue.toString().trim()).append("'");
		} else if (condition.equals(OP_IN)) {// in
			if (propertyValue.toString().trim().startsWith("(")
					&& propertyValue.toString().trim().endsWith(")")) {
				sb.append(propertyValue.toString().trim());
			} else {
				sb.append("(").append(propertyValue.toString().trim()).append(
						")");
			}
		} else {// 不是like
			try {// 试着将propertyValue转换成dobule,如果失败的话,说明不是数字
				Double.valueOf(propertyValue.toString());
				sb.append(propertyValue.toString().trim());
			} catch (NumberFormatException e) {// 不是数字的要用'引起来
				sb.append("'").append(propertyValue.toString().trim()).append(
						"'");
			}
		}
		return sb;
	}

	public String getPropertyName() {
		return propertyName;
	}

	/** 设置属性名 **/
	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

	public String getCondition() {
		return condition;
	}

	/** 设置符号 = > 等等 **/
	public void setCondition(String condition) {
		this.condition = condition;
	}

	public Object getPropertyValue() {
		return propertyValue;
	}

	/** 设置属性值 **/
	public void setPropertyValue(Object propertyValue) {
		this.propertyValue = propertyValue;
	}

	public String getAndOr() {
		return andOr;
	}

	/** 设置AND/OR **/
	public void setAndOr(String andOr) {
		this.andOr = andOr;
	}
}
